Ext.onReady(function()
{
	var root = new Ext.tree.AsyncTreeNode({
            id:'0',
        	text:'根'});
    var store = new Ext.data.SimpleStore(
    {fields:['report'],
     data:[['Pasry'],['Pasents']]
    });

    var combo_config =
    {
        id: 'chart_combo_list',
        store: store,
        displayField: 'report',
        xtype: 'combo',
        mode: 'local',
        triggerAction: 'all',
        width: 100,
        emptyText: 'Please select report type..',
        selectOnFocus: true
    };
    var tree = new Ext.tree.ColumnTree(
    {
        id: 'tree',
        region:'center',
        rootVisible: false,
        autoScroll: true,
        expandable: false,
        enableDD: true,
        title:false,
        tbar:
        [
            {
                text: '保存',
                tooltip: '保存',
                listeners:
                {
                    'click': function()
                    {
                        var json = tree.toJsonString(null, function(key, val)
                        {
                            return (key == 'leaf' || key == 'id' || key == 'task' || key == 'user');
                        },
                        {
                            menu_item: 'task',
                            menu_url: 'user'
                        });
                        alert(json);
                    },
                    scope: this
                }
            },'-',
            {
                text: '添加文件夹',
                tooltip: '添加文件夹',
                listeners:
                {
                    'click': function()
                    {
                        var selectedItem = tree.getSelectionModel().getSelectedNode();
                        if (! selectedItem)
                        {
                            selectedItem = tree.getRootNode();
                        }
                        handleCreate = function (btn, text, cBoxes)
                        {
                            if (btn == 'ok' && text)
                            {
                                var newNode = new Ext.tree.TreeNode(
                                {
                                    menu_item: text,
                                    menu_url: '',
                                    leaf: false,
                                    expandable: true,
                                    uiProvider: Ext.tree.ColumnNodeUI
                                });
                                if (selectedItem.isLeaf())
                                {
                                    selectedItem.parentNode.insertBefore(newNode, selectedItem.nextSibling);
                                }
                                else
                                {
                                    selectedItem.insertBefore(newNode, selectedItem.firstChild);
                                }
                            }
                        }
                        Ext.MessageBox.show(
                        {
                            title: '添加一个文件夹',
                            msg: '文件夹名称:',
                            buttons: Ext.MessageBox.OKCANCEL,
                            prompt: true,
                            fn: handleCreate
                        });
                    }
                }
            },'-',
            {
                text: '添加叶子',
                tooltip: '添加叶子',
                listeners:
                {
                    'click': function()
                    {
                        var selectedItem = tree.getSelectionModel().getSelectedNode();
                        if (!selectedItem)
                        {
                            Ext.Msg.alert('警告', '请先选择一个');
                            return false;
                        }
                        handleCreate = function (btn, text, cBoxes)
                        {
                            if (btn == 'ok' && text)
                            {
                                var newNode = new Ext.tree.TreeNode(
                                {
                                    menu_item: text,
                                    menu_url: '',
                                    leaf: true,
                                    allowChildren: false,
                                    uiProvider: Ext.tree.ColumnNodeUI
                                });
                                if (selectedItem.isLeaf())
                                {
                                    selectedItem.parentNode.insertBefore(newNode, selectedItem.nextSibling);
                                }
                                else
                                {
                                    selectedItem.insertBefore(newNode, selectedItem.firstChild);
                                }
                            }
                        }
                        Ext.MessageBox.show(
                        {
                            title: '添加一个叶子',
                            msg: '叶子名称:',
                            buttons: Ext.MessageBox.OKCANCEL,
                            prompt: true,
                            fn: handleCreate
                        });
                    }
                }
            },'-',
            {
                text: '删除一个项目',
                tooltip: '删除一个项目',
                listeners:
                {
                  'click': function()
                  {
                      var selectedItem = tree.getSelectionModel().getSelectedNode();
                      if (!selectedItem)
                      {
                          Ext.Msg.alert('警告', '请选择一个删除');
                          return false;
                      }
                      handleDelete = function (btn)
                      {
                          if (btn == 'ok')
                          {
                              selectedItem.remove();
                          }
                      }
                      Ext.MessageBox.show(
                      {
                          title: '确定？',
                          msg: '确定吗?',
                          buttons: Ext.MessageBox.OKCANCEL,
                          fn: handleDelete
                      });
                  }
                }
            },'-'
        ],
        columns:
        [
            {
                header: '模型名称',
                width: 200,
                dataIndex: 'task'
            },
            {
                header: '所用表',
                width: 150,
                dataIndex: 'duration'
            },
            {
                header: '扩展',
                width: 120,
                dataIndex: 'user'
            },is_show
        ],
       loader: new Ext.tree.TreeLoader({
            dataUrl:'web_config.php?require=tree',
            uiProviders:{
                'col': Ext.tree.ColumnNodeUI
            }
        }),
        root: root
    });
    tree.expandAll();
	var viewport = new Ext.Viewport({
		layout:'border',
			items    : [
			tree
		]
	});
});